/**
 * Created with JetBrains PhpStorm.
 * User: Artem
 * Date: 20.03.13
 * Time: 14:16
 * To change this template use File | Settings | File Templates.
 */



Ext.define("Pro.controller.Console", {
    extend: "Ext.app.Controller",

    refs: [
        {
            ref: "eventLogPanel",
            selector: "consolepanel #eventLogPanel"
        },
        {
            ref: "sqlPanel",
            selector: "consolepanel #sqlPanel"
        },
        {
            ref: "eventLogArea",
            selector: "consolepanel #eventLogArea"
        },
        {
            ref: "sqlFormPanel",
            selector: "consolepanel #sqlFormPanel"
        },
        {
            ref: "requestTab",
            selector: "consolepanel #request"
        },
        {
            ref: "respondTab",
            selector: "consolepanel #respond"
        },
        {
            ref: "sqlResultGridPanel",
            selector: "consolepanel #sqlResultGridPanel"
        }
    ],

    init: function(){
        this.control({
            "consolepanel #eventLogButton": {
                click: this.onEventLogButtonClick
            },
            "consolepanel #sqlConsoleButton": {
                click: this.onsqlConsoleButtonClick
            },
            "consolepanel #clearHistoryButton": {
                click: this.onClearHistoryButtonClick
            },
            "consolepanel #executeButton": {
                click: this.onExecuteButtonClick
            },
            "consolepanel #updateServerDataButton": {
                click: this.onUpdateServerDataButtonClick
            }
        });
    },

    onEventLogButtonClick: function(btn, e, eOpts ){
        var eventLogPanel = this.getEventLogPanel();
        if (btn.pressed){
            eventLogPanel.show();
        } else {
            eventLogPanel.hide();
        }
    },

    onsqlConsoleButtonClick: function(btn, e, eOpts ){
        var sqlPanel = this.getSqlPanel();
        if (btn.pressed){
            sqlPanel.show();
        } else {
            sqlPanel.hide();
        }
    },

    onClearHistoryButtonClick: function(){
        var eventLogArea = this.getEventLogArea();
        eventLogArea.setValue("");
    },

    onExecuteButtonClick: function(){
        var sqlFormPanel = this.getSqlFormPanel();
        var thisController = this;
        sqlFormPanel.getForm().submit({
            url: "globallogic/sql",
            success: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_SUCCESS,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
                thisController.printTable(action);
            },
            failure: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_FAILURE,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            }
        });
    },

    printRespond: function(action){
        var respondTab = this.getRespondTab();
        var eventLogArea = this.getEventLogArea();
        eventLogArea.setValue(eventLogArea.getValue() + (new Date()).toLocaleString() + " " + action.result.message + "\n");
        respondTab.setValue(action.response.responseText);
    },

    printRequest: function(action){
        var requestTab = this.getRequestTab();
        var req = "";
        req += ("url:\t\t\t" + action.response.request.options.url + "\n");
        req += ("method:\t\t" + action.response.request.options.method + "\n");
        req += "params: \n"
        req += ("\tsql:\t\t" + action.response.request.options.params.sql + "\n");
        requestTab.setValue(req);
    },

    createStoreFromSqlRespond: function(root){

        var store = {
            fields: [],
            data: []
        };
        var tempData;
        var tempFieldName;

        for (var i in root[0]){
            store.fields.push(i);
        };
        for (var i = 0; i < root.length; i++){
            tempData = {};
            for (var j = 0; j < store.fields.length; j++){
                tempFieldName = store.fields[j];
                tempData[tempFieldName] = root[i][tempFieldName];
            };
            store.data.push(tempData);
        }

        return store;
    },

    createColumnsFromSqlRespond: function(root){

        var columns = [
            {
                xtype: "rownumberer"
            }
        ];
        var tempColumn;

        for (var i in root[0]){
            tempColumn = {
                header: i,
                dataIndex: i,
                flex: 1
            };
            columns.push(tempColumn);
        }

        return columns;
    },

    printTable: function(action){
        var sqlResultGridPanel = this.getSqlResultGridPanel();
        var store = this.createStoreFromSqlRespond(action.result.root);
        var columns = this.createColumnsFromSqlRespond(action.result.root);
        var sqlTable = Ext.create("Pro.view.SqlRespondPanel", store, columns);
        sqlResultGridPanel.removeAll();
        sqlResultGridPanel.add(sqlTable);
    },

    onUpdateServerDataButtonClick: function(){
        Ext.create("Pro.view.SecuritySystem");
    }
});